#include "app.h"
#include "Timer.h"
#include "gizwits_product.h"
#include "configs.h"
#include "ADC.h"
#include "dht11.h"
#include "act.h"
#include "stepper.h"
STRAppType g_app_stRegs;
void fnApp_Init(void);
void fnApp_RealTime(void);
void fnApp_ReadAttr(STRAppAttrType *attrs);
void fnApp_Init(void){
	memset(&g_app_stRegs, 0, sizeof(STRAppType));
	g_app_stRegs.wificonnecttimer = 10;
	g_app_stRegs.connecttimes = 5;
	fnConfigs_Init();
	fnConfigs_ConfigDisplayData(&g_app_stRegs.attrs);
	fnACT_Init();
	fnStepper_Init();
	
}
void fnApp_ReadAttr(STRAppAttrType *attrs)
{	
	DHT11_Read_Data(&attrs->temp, &attrs->himi);
	attrs->gz = 4096 - get_Adc_Value(0x04);
	attrs->yw = get_Adc_Value(0x07) * 70 * (3.3/4096);
	attrs->CO = get_Adc_Value(0x05) * 20 * (3.3/4096);
	attrs->airquality = get_Adc_Value(0x06) * 300 * (3.3/4096);
}
void fnApp_SM(void){
	STRAppAttrType attrs;
	if(g_tm_stTimerFlag.Bits.bTimer10ms){
		if(g_app_stRegs.timer > 0){
			g_app_stRegs.timer --;
		}
		if(g_app_stRegs.reportlimtimer){
			g_app_stRegs.reportlimtimer--;
		}
		if(g_app_stRegs.reporttimer){
			g_app_stRegs.reporttimer--;
		}
	}
	if(g_tm_stTimerFlag.Bits.bTimer100ms){
		fnApp_ReadAttr(&attrs);
		g_app_stRegs.attrs.temp = attrs.temp;
		g_app_stRegs.attrs.himi = attrs.himi;
		g_app_stRegs.attrs.yw = attrs.yw;
		g_app_stRegs.attrs.CO = attrs.CO;
		g_app_stRegs.attrs.airquality = attrs.airquality;
		g_app_stRegs.attrs.gz = attrs.gz;
		g_app_stRegs.attrready = TRUE;
	}
	if(g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_MODE] == CONFIG_MODE_LOCAL){
		if(g_app_stRegs.attrready){
			if(g_app_stRegs.attrs.gz > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_GZ]){
				fnACT_LEDCon(FALSE);
				fnStepper_Con(FALSE);
				g_app_stRegs.attrs.curtain = FALSE;
				g_app_stRegs.attrs.led = FALSE;
			}else{
				fnACT_LEDCon(TRUE);
				fnStepper_Con(TRUE);
				g_app_stRegs.attrs.curtain = TRUE;
				g_app_stRegs.attrs.led = TRUE;
			}
			if((g_app_stRegs.attrs.temp > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_TEMP])
				|| (g_app_stRegs.attrs.himi > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_HUM])
					|| (g_app_stRegs.attrs.CO > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M7])
						||(g_app_stRegs.attrs.yw > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M2])
							||(g_app_stRegs.attrs.airquality > g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_M135])){
				fnACT_FANCon(TRUE);
				fnACT_BEEPCon(TRUE);
				g_app_stRegs.attrs.fan = TRUE;
				g_app_stRegs.attrs.beep = TRUE;				
			}else{
				fnACT_FANCon(FALSE);
				fnACT_BEEPCon(FALSE);
				g_app_stRegs.attrs.fan = FALSE;
				g_app_stRegs.attrs.beep = FALSE;		
			}
		}
	}else{
		fnACT_LEDCon(g_app_stRegs.attrs.led);
		fnStepper_Con(g_app_stRegs.attrs.curtain);
		fnACT_FANCon(g_app_stRegs.attrs.fan);
		fnACT_BEEPCon(g_app_stRegs.attrs.beep);
	}
	
}

void fnApp_WiFiCon(void){
	if(g_tm_stTimerFlag.Bits.bTimer100ms){
		if(g_app_stRegs.wificonnecttimer > 0){
			g_app_stRegs.wificonnecttimer --;
		}
	}
	if(0 == g_app_stRegs.wificonnecttimer){
		if(g_app_stRegs.wificonnected == 0){
			gizwitsSetMode(WIFI_SOFTAP_MODE);
		}
		if(g_app_stRegs.connecttimes > 0){
			g_app_stRegs.wificonnecttimer = 20;
			g_app_stRegs.connecttimes --;
		}else{
			g_app_stRegs.wificonnecttimer = 2000;
		}
	}
}
void fnApp_RealTime(void){
	fnApp_SM();
	fnApp_WiFiCon();
	fnConfigs_RealTime();
}
#define APP_BITDATA_NUM 3
void fnApp_HandlActionData(U8 *data, U16 len){
	U8 i;
	U16 flags;
	U8 dataoffset = 2;
	U8 val;
	U8 hasbitdata = FALSE;
	flags = (U16)data[0];
	flags = flags << 8;
	flags |= (U16)data[1];
	
	for(i = 0; i < 8; i++){
		if(flags &(1 << i)){
			if(i >= APP_BITDATA_NUM){
				if(hasbitdata){
					dataoffset ++;
					hasbitdata = FALSE;
				}
			}
			if(i < APP_BITDATA_NUM){
				val = data[dataoffset];
				hasbitdata = TRUE;
				if(g_config_stRegs.configvalue[CONFIG_PARAM_OFFSET_MODE] == CONFIG_MODE_REMOTE){
					if(i == 0){
						if(val){
							g_app_stRegs.attrs.led = TRUE;
						}else{
							g_app_stRegs.attrs.led= FALSE;
						}
					}
					
					if(i == 1){
						if(val){
							g_app_stRegs.attrs.curtain = TRUE;
						}else{
							g_app_stRegs.attrs.curtain = FALSE;
						}

					}
					if(i == 2){
						if(val){
							g_app_stRegs.attrs.fan = TRUE;
						}else{
							g_app_stRegs.attrs.fan = FALSE;
						}

					}
					}
					
				}
			
		}
	}
#if(0)
	U8 i;
	U16 flags;
	U8 dataoffset = 1;
	U8 val;
	U8 hasbitdata = FALSE;
	flags = (U16)data[0];
	
	for(i = 0; i < 8; i++){
		if(flags &(1 << i)){
			if(i > APP_BITDATA_NUM){
				if(hasbitdata){
					dataoffset ++;
					hasbitdata = FALSE;
				}
			}
			if(i > APP_BITDATA_NUM){
				//handle here
				dataoffset ++;
			}
			if(i < APP_BITDATA_NUM){
				val = data[dataoffset];
				hasbitdata = TRUE;
					if(i == 0){
						if(val){
							g_app_stRegs.attrs.led = TRUE;
						}else{
							g_app_stRegs.attrs.led = FALSE;
						}
					}
					
					if(i == 1){
						if(val){
							g_app_stRegs.attrs.curtain = TRUE;
						}else{
							g_app_stRegs.attrs.curtain = TRUE;
						}

					}
					if(i == 2){
						if(val){
							g_app_stRegs.attrs.fan = TRUE;
						}else{
							g_app_stRegs.attrs.fan = TRUE;
						}

					}
				}
			
			
		}
	}
#endif
}
U8 reportdatabackbuff[100] = {0};
U32 reportdatabacklen;
U8 fnAppGetData(U8 *data, U32 *len){
	U32 value;
	data[0] = 0x01;
	data[1] = 0xFF;
	value = 0;
	if(g_app_stRegs.attrs.led){
		value |= 0x01;
	}
	if(g_app_stRegs.attrs.curtain){
		value |= 0x02;
	}
	if(g_app_stRegs.attrs.fan){
		value |= 0x04;
	}
	data[2] = (U8)(value);
	value = (U16)g_app_stRegs.attrs.temp;
	data[3] = (U8)(value);
	
	
	
	
	value = (U16)g_app_stRegs.attrs.himi;
	data[4] = (U8)(value >> 8);
	data[5] = (U8)(value & 0xff);


	value = (U16)g_app_stRegs.attrs.gz;
	data[6] = (U8)(value >> 8);
	data[7] = (U8)(value & 0xff);
	
	value = (U16)g_app_stRegs.attrs.yw;
	data[8] = (U8)(value >> 8);
	data[9] = (U8)(value & 0xff);
	
	value = (U16)g_app_stRegs.attrs.CO;
	data[10] = (U8)(value >> 8);
	data[11] = (U8)(value & 0xff);
	
	value = (U16)g_app_stRegs.attrs.airquality;
	data[12] = (U8)(value >> 8);
	data[13] = (U8)(value & 0xff);

	*len = 14;
	return(TRUE);
}
U8 fnApp_CheckReport(void){
	U8 ret = FALSE;
	U8 nreportdata[10];
	U32 nreportlen;
	fnAppGetData(nreportdata, &nreportlen);
	if((nreportlen != reportdatabacklen)
		||(0 != memcmp(nreportdata, reportdatabackbuff,  nreportlen))){
		ret = TRUE;
		reportdatabacklen = nreportlen;
		memcpy(reportdatabackbuff, nreportdata, nreportlen);
			
	}
	if(g_app_stRegs.reporttimer == 0){
		
		ret = TRUE;
	}
	
	if(ret){
		
	}
	if(ret){
		if(g_app_stRegs.reportlimtimer > 0){
			ret = FALSE;
		}
		else{
			g_app_stRegs.reporttimer = 1000;
			g_app_stRegs.reportlimtimer = 50;
		}
	}
	return(ret);
}
void fnApp_GetReport(U8 *data, U32 *len){
	fnAppGetData(data, len);
	
	
}
void fnApp_WFCallBack(U8 status){
	g_app_stRegs.wificonnected = status;
}
void fnApp_WFRSSI(U8 rssi){
	g_app_stRegs.wifirssi = rssi;
}

